package image;

import gui.Settings;
import javafx.beans.binding.DoubleBinding;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.scene.Node;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;

/**
 * A layer can typically contain a shape, filled with a color, with a filter, etc. 
 * @author Etienne Bruines
 * @author Bas Dobbelsteen
 */
public abstract class Layer 
{
	protected DoubleProperty xProp;
	protected DoubleProperty yProp;
	
	protected DoubleProperty widthProp;
	protected DoubleProperty heightProp;
	
	private String title;
	
	// ============
	// Constructors
	// ============
	
	public Layer(String t)
	{
		title = t;
		xProp = new SimpleDoubleProperty();
		yProp = new SimpleDoubleProperty();
		widthProp = new SimpleDoubleProperty();
		heightProp = new SimpleDoubleProperty();
	}
	
	protected void setBinding()
	{
		DoubleBinding xBinding = new DoubleBinding()
		{
			{
				bind(getContent().boundsInParentProperty());
			}
			@Override protected double computeValue()
			{
				return getContent().getBoundsInParent().getMinX();
			}
		};
		xProp.bind(xBinding);
		DoubleBinding yBinding = new DoubleBinding()
		{
			{
				bind(getContent().boundsInParentProperty());
			}
			@Override protected double computeValue()
			{
				return getContent().getBoundsInParent().getMinY();
			}
		};
		yProp.bind(yBinding);
		
		DoubleBinding widthBinding = new DoubleBinding()
		{
			{
				bind(getContent().boundsInParentProperty());
			}
			@Override protected double computeValue()
			{
				return getContent().getBoundsInParent().getWidth();
			}
		};
		widthProp.bind(widthBinding);
		DoubleBinding heightBinding = new DoubleBinding()
		{
			{
				bind(getContent().boundsInParentProperty());
			}
			@Override protected double computeValue()
			{
				return getContent().getBoundsInParent().getHeight();
			}
		};
		heightProp.bind(heightBinding);
		
		
	}
	
	public static Rectangle createBackground(int width, int height)
	{
		Rectangle l = new Rectangle(width, height, Color.WHITE);
		return l;
	}
	
	// ============
	// Setters
	// ============
	
	
	public void setTitle(String t)
	{
		title = t;
	}
	
	// ============
	// Getters
	// ============
	
	public static Layer getShape(Settings s, double x, double y, double width, double height)
	{
		Layer l;
		switch (s.getMode())
		{
		case Rectangle:
			l = new image.Rectangle("Rectangle", s.getForeColor(), s.getBackColor(), s.getStrokeWidth(), x, y, width, height);
			break;
		case Ellipse:
			l = new Ellipse("Ellipse", s.getForeColor(), s.getBackColor(), s.getStrokeWidth(), x + width / 2, y + height / 2, width / 2, height / 2);
			break;
		case Triangle:
			l = new Triangle("Triangle", s.getForeColor(), s.getBackColor(), s.getStrokeWidth(), s.trianglePoints());
			break;
		case Line:
			l = new Line("Line", s.getBackColor(), s.getForeColor(), s.getStrokeWidth(), x, y, x + width, y + height);
			break;
		case Text:
			l = new Texting("Text", s.getBackColor(), s.getForeColor(), x, y, x + width, y + height);
			break;
		default:
			l = null;
			break;
		}
		return l;
	}
	
	
	public String getTitle()
	{
		return title;
	}
	
	public abstract Node getContent();
	
	public DoubleProperty xProperty()
	{
		return xProp;
	}
	public DoubleProperty yProperty()
	{
		return yProp;
	}
	
	public DoubleProperty widthProperty()
	{
		return widthProp;
	}
	public DoubleProperty heightProperty()
	{
		return heightProp;
	}
	
	public abstract void setBorder(Color c);
	public abstract void setBackground(Color c);
	
	public abstract int getWidth();
	
	public abstract int getHeight();
	
	public abstract int getX();
	
	public abstract int getY();
	
	public abstract void setStroke (double width);
	
	@Override public String toString()
	{
		return this.title;
	}
}
